using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Globalization;

namespace The_LarX
{
    public class Data
    {
        public string Name;
        public object Object;

        public override string ToString()
        {
            return Name;
        }

        public Data(string Name, object Object)
        {
            this.Name = Name;
            this.Object = Object;
        }
        public Data() { }

        public static string FromVector3ToString(Vector3 vector)
        {
            string obj = "Vector3:" + vector.X.ToString(CultureInfo.InvariantCulture) + ":" + 
                vector.Y.ToString(CultureInfo.InvariantCulture) + ":" + 
                vector.Z.ToString(CultureInfo.InvariantCulture);
            return obj;
        }
        public void TestData()
        {
            if (Object is Vector2)
            {
                Vector2 vector = (Vector2)Object;
                string obj = "Vector2:" + vector.X.ToString(CultureInfo.InvariantCulture) + ":" + vector.Y.ToString(CultureInfo.InvariantCulture);
                this.Object = obj;
            }
            if (Object is Vector3)
            {
                Vector3 vector = (Vector3)Object;
                string obj = FromVector3ToString(vector);
                this.Object = obj;
            }
            if (Object is Vector4)
            {
                Vector4 vector = (Vector4)Object;
                string obj = "Vector4:" + vector.X.ToString(CultureInfo.InvariantCulture) + ":" + vector.Y.ToString(CultureInfo.InvariantCulture) + ":" + vector.Z.ToString(CultureInfo.InvariantCulture) + ":" + vector.W.ToString(CultureInfo.InvariantCulture);
                this.Object = obj;
            }
            if (Object is Color)
            {
                Color Color = (Color)Object;
                string obj = "Color:" + Color.R + ":" + Color.G + ":" + Color.B + ":" + Color.A;
                this.Object = obj;
            }
            if (Object is Matrix)
            {
                Matrix matrix = (Matrix)Object;
                string obj = "Matrix:"
                    + matrix.M11.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M12.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M13.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M14.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M21.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M22.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M23.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M24.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M31.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M32.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M33.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M34.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M41.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M42.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M43.ToString(CultureInfo.InvariantCulture) + ":"
                    + matrix.M44.ToString(CultureInfo.InvariantCulture) + ":";
                this.Object = obj;
            }
            if (Object is float)
            {
                float f = (float)Object;
                string obj = "Float:" + f.ToString(CultureInfo.InvariantCulture);
                this.Object = obj;
            }
            if (Object is List<string>)
            {
                List<string> List = (List<string>)Object;
                string St = "List.string:";

                for (int i = 0; i < List.Count; i++)
                {
                    St += List[i];
                    if (i != List.Count - 1)
                    {
                        St += ":";
                    }
                }
                this.Object = St;
            }
            if (Object is List<int>)
            {
                List<int> list = (List<int>)Object;
                string ints = "List.int:";
                
                for (int i = 0; i < list.Count; i++)
                {
                    ints += list[i].ToString(CultureInfo.InvariantCulture);
                    if (i != list.Count - 1)
                    {
                        ints += ":";
                    }
                }
                this.Object = ints;
            }
            if (Object is Rectangle)
            {
                Rectangle Rect = (Rectangle)Object;
                string rectangle = "Rect:";
                rectangle += Rect.X.ToString(CultureInfo.InvariantCulture) + ":";
                rectangle += Rect.Y.ToString(CultureInfo.InvariantCulture) + ":";
                rectangle += Rect.Width.ToString(CultureInfo.InvariantCulture) + ":";
                rectangle += Rect.Height.ToString(CultureInfo.InvariantCulture);
            }
        }

        public static Vector3 FromStringToVector3(string str)
        {
            string[] vectorValues = str.Split(":".ToCharArray());
            Vector3 Vector = new Vector3(float.Parse(vectorValues[1], CultureInfo.InvariantCulture), float.Parse(vectorValues[2], CultureInfo.InvariantCulture), float.Parse(vectorValues[3], CultureInfo.InvariantCulture));
            return Vector;
        }
        public Data GetData()
        {
            if (Object is string)
            {
                string Name = (string)Object;
                if (Name.StartsWith("Vector2:"))
                {
                    string[] vectorValues = Name.Split(":".ToCharArray());
                    Vector2 Vector;
                    Vector = new Vector2(float.Parse(vectorValues[1], CultureInfo.InvariantCulture), float.Parse(vectorValues[2], CultureInfo.InvariantCulture));
                    this.Object = Vector;
                }
                if (Name.StartsWith("Vector3:"))
                {
                    Vector3 Vector = FromStringToVector3(Name);
                    this.Object = Vector;
                }
                if (Name.StartsWith("Vector4:"))
                {
                    string[] vectorValues = Name.Split(":".ToCharArray());
                    Vector4 Vector = new Vector4(float.Parse(vectorValues[1], CultureInfo.InvariantCulture), float.Parse(vectorValues[2], CultureInfo.InvariantCulture), float.Parse(vectorValues[3], CultureInfo.InvariantCulture), float.Parse(vectorValues[4], CultureInfo.InvariantCulture));
                    this.Object = Vector;
                }
                if (Name.StartsWith("Color:"))
                {
                    string[] colorValues = Name.Split(":".ToCharArray());
                    Color Color = new Color((byte)float.Parse(colorValues[1], CultureInfo.InvariantCulture), (byte)float.Parse(colorValues[2], CultureInfo.InvariantCulture), (byte)float.Parse(colorValues[3], CultureInfo.InvariantCulture));
                    this.Object = Color;
                }
                if (Name.StartsWith("Matrix:"))
                {
                    string[] matrixValues = Name.Split(":".ToCharArray());
                    Matrix Matrix = new Matrix(
                        float.Parse(matrixValues[1], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[2], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[3], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[4], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[5], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[6], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[7], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[8], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[9], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[10], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[11], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[12], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[13], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[14], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[15], CultureInfo.InvariantCulture),
                        float.Parse(matrixValues[16], CultureInfo.InvariantCulture));
                    this.Object = Matrix;
                }
                if (Name.StartsWith("Float:"))
                {
                    string[] floatValues = Name.Split(":".ToCharArray());
                    float f = float.Parse(floatValues[1], CultureInfo.InvariantCulture);
                    this.Object = f;
                }
                if (Name.StartsWith("List.string:"))
                {
                    string[] listValues = Name.Split(":".ToCharArray()[0]);
                    List<string> list = new List<string>();
                    for (int i = 1; i < listValues.Length; i++)
                    {
                        list.Add(listValues[i]);
                    }
                    this.Object = list;
                }
                if (Name.StartsWith("List.int:"))
                {
                    string[] listValues = Name.Split(":".ToCharArray()[0]);
                    List<int> list = new List<int>();
                    for (int i = 1; i < listValues.Length; i++)
                    {
                        list.Add(int.Parse(listValues[i], CultureInfo.InvariantCulture));
                    }
                    this.Object = list;
                }
                if (Name.StartsWith("Rect:"))
                {
                    string[] RectValues = Name.Split(":"[0]);
                    Rectangle Rectangle = new Rectangle(int.Parse(RectValues[1], CultureInfo.InvariantCulture),
                        int.Parse(RectValues[2], CultureInfo.InvariantCulture),
                        int.Parse(RectValues[3], CultureInfo.InvariantCulture),
                        int.Parse(RectValues[4], CultureInfo.InvariantCulture));
                }
            }
            return this;
        }
    }
}